package com.pjedu.nbmalladminsystem.web;

import com.pjedu.nbmallcommon.common.Constants;
import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.Result;
import com.pjedu.nbmallcommon.utils.ResultGenerator;
import com.pjedu.nbmallshop.entity.MallGoodsInfo;
import com.pjedu.nbmallshop.service.MallGoodsCategoryService;
import com.pjedu.nbmallshop.service.MallGoodsInfoService;
import java.util.HashMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**这里是类的注释*/
@RestController
@RequestMapping("/admin")
public class MallGoodsInfoController {

    @Resource
    private MallGoodsInfoService mallGoodsInfoService;
    // @Resource
    // private MallGoodsCategoryService mallGoodsCategoryService;

    /**
     * 列表
     */
    @RequestMapping(value = "/goods/list", method = RequestMethod.GET)
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        HashMap pageUtil = new HashMap(params);
        return ResultGenerator.genSuccessResult(mallGoodsInfoService.getMallGoodsPage(pageUtil));
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/goods/save", method = RequestMethod.POST)
    public Result save(@RequestBody MallGoodsInfo mallGoods) {
        if (StringUtils.isEmpty(mallGoods.getGoodsName())
                || StringUtils.isEmpty(mallGoods.getGoodsIntro())
                || StringUtils.isEmpty(mallGoods.getTag())
                || Objects.isNull(mallGoods.getOriginalPrice())
                || Objects.isNull(mallGoods.getGoodsCategoryId())
                || Objects.isNull(mallGoods.getSellingPrice())
                || Objects.isNull(mallGoods.getStockNum())
                || Objects.isNull(mallGoods.getGoodsSellStatus())
                || StringUtils.isEmpty(mallGoods.getGoodsCoverImg())
                || StringUtils.isEmpty(mallGoods.getGoodsDetailContent())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = mallGoodsInfoService.saveMallGoods(mallGoods);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/goods/update", method = RequestMethod.POST)
    public Result update(@RequestBody MallGoodsInfo mallGoods) {
        if (Objects.isNull(mallGoods.getGoodsId())
                || StringUtils.isEmpty(mallGoods.getGoodsName())
                || StringUtils.isEmpty(mallGoods.getGoodsIntro())
                || StringUtils.isEmpty(mallGoods.getTag())
                || Objects.isNull(mallGoods.getOriginalPrice())
                || Objects.isNull(mallGoods.getSellingPrice())
                || Objects.isNull(mallGoods.getGoodsCategoryId())
                || Objects.isNull(mallGoods.getStockNum())
                || Objects.isNull(mallGoods.getGoodsSellStatus())
                || StringUtils.isEmpty(mallGoods.getGoodsCoverImg())
                || StringUtils.isEmpty(mallGoods.getGoodsDetailContent())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = mallGoodsInfoService.updateMallGoods(mallGoods);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 详情
     */
    @GetMapping("/goods/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        MallGoodsInfo goods = mallGoodsInfoService.getMallGoodsById(id);
        if (goods == null) {
            return ResultGenerator.genFailResult(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        return ResultGenerator.genSuccessResult(goods);
    }

    /**
     * 批量修改销售状态
     */
    @RequestMapping(value = "/goods/status/{sellStatus}", method = RequestMethod.PUT)
    public Result delete(@RequestBody Long[] ids, @PathVariable("sellStatus") int sellStatus) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (sellStatus != Constants.SELL_STATUS_UP && sellStatus != Constants.SELL_STATUS_DOWN) {
            return ResultGenerator.genFailResult("状态异常！");
        }
        if (mallGoodsInfoService.batchUpdateSellStatus(ids, sellStatus)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("修改失败");
        }
    }

}